﻿@{
    ViewBag.Title = "DevicePartDetail";
    Layout = "~/Views/Shared/_Form.cshtml";
}
@System.Web.Optimization.Styles.Render("~/Content/scripts/plugins/jqgrid/css")
@System.Web.Optimization.Scripts.Render("~/Content/scripts/plugins/jqgrid/js")
<script>
    var keyValue = request('keyValue');
    var Rowid = request('Rowid');
    var DevicePartsIds = request('DevicePartsIds');
    var _TypeId = request('Type');
    $(function () {
        GetGrid();
        //获取表单
        if (!!Rowid) {
            $("#layout").remove();
            var rowdata = $.currentIframe().$("#DevicePartTable").jqGrid("getRowData", Rowid);
            $("#DevicePartDetail").SetWebControls(rowdata);
            if (!rowdata.BeginDate)
                $("#BeginDate").val("@DateTime.Now.ToString("yyyy-MM-dd")");
            if (!rowdata.BeginDate)
                $("#EndDate").val("@DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")");
        }
        else {
            $("#DevicePartDetail").remove();
        }
        //if (!!keyValue) {
        //    $.SetForm({
        //        url: "../../ResourceManage/RS_Device/GetFormJson",
        //        param: { keyValue: keyValue },
        //        success: function (data) {
        //            //list = data.list;
        //            //$("#OrganizeId").ComboBoxTreeSetValue(data.OrganizeId);
        //            //$("#DeviceName").val(data.Name).attr("disabled", "disabled");
        //            //$("#DeviceCode").val(data.Code).attr("disabled", "disabled");
        //            //$("#DeviceModelNo").val(data.ModelNo).attr("disabled", "disabled");
        //        }
        //    });
        //};
    });

    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {};
        queryJson["condition"] = $("#queryCondition").find('.dropdown-text').attr('data-value');
        queryJson["keyword"] = $("#txt_Keyword").val();
        queryJson["TypeId"] = _TypeId;
        queryJson["DevicePartsIds"] = DevicePartsIds;
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_DeviceType/GetPartsList",
            postData: {
                queryJson: JSON.stringify(queryJson)
            },
            datatype: "json",
            height: $(window).height() - 91.5,
            autowidth: true,
            colModel: [
                { label: "主键", name: "Id", index: "Id", width: 100, align: "left", hidden: true },
                { label: "零配件名称", name: "Name", index: "Name", width: 100, align: "left" },
                { label: "生产厂商", name: "Factory", index: "Factory", width: 100, align: "left" },
                { label: "规格型号", name: "ModelNo", index: "ModelNo", width: 100, align: "left" },
                { label: "价格", name: "Price", index: "Price", width: 100, align: "left" },
                { label: "附加说明", name: "Description", index: "Description", width: 200, align: "left" }
            ],
            viewrecords: true,
            multiselect: true,
            rowNum: 10000,
            rownumbers: true,
            shrinkToFit: false,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {};
            queryJson["condition"] = $("#queryCondition").find('.dropdown-text').attr('data-value');
            queryJson["keyword"] = $("#txt_Keyword").val();
            queryJson["TypeId"] = _TypeId;
            queryJson["DevicePartsIds"] = DevicePartsIds;
            $gridTable.jqGrid('setGridParam', {
                url: "../../ResourceManage/RS_DeviceType/GetPartsList",
                postData: {
                    queryJson: JSON.stringify(queryJson)
                },
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }

    //新增设备零配件
    function btn_addParts() {
        dialogOpen({
            id: "PartsForm",
            title: '新增设备零配件',
            url: '/ResourceManage/RS_DeviceType/PartsForm?switchType=1',
            width: "800px",
            height: "500px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    function btn_editParts() {
        debugger;
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "PartsForm",
                title: '编辑设备零配件',
                url: '/ResourceManage/RS_DeviceType/PartsForm?switchType=1&keyValue=' + keyValue,
                width: "800px",
                height: "500px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    function btn_deleteParts() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: "../../ResourceManage/RS_DeviceType/RemovePartsForm",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的参数！', 0);
        }
    }

    //保存表单
    function AcceptClick(callback) {
        if (!$('#form1').Validform()) {
            return false;
        }
        var postData;
        if (!Rowid) {
            postData = [];
            var Rowids = $("#gridTable").jqGrid('getGridParam', 'selarrrow');
            for (var i = 0; i < Rowids.length; i++) {
                var rowData = $("#gridTable").jqGrid('getRowData', Rowids[i]);
                rowData.DevicePartsId = rowData.Id;
                rowData.Num = 1;
                rowData.CreateDate = "@DateTime.Now.ToString("yyyy-MM-dd")";
                delete rowData.Id;
                postData.push(rowData);
            }
        }
        else
            postData = $("#DevicePartDetail").GetWebControls("");
        callback(postData);
        dialogMsg("操作成功。", 1);
        dialogClose();
    }
</script>

<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-center">
        <div class="titlePanel">
            <div class="title-search">
                <table>
                    <tr>
                        <td>
                            <div class="btn-group" id="queryCondition">
                                <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a data-value="Name">名称</a></li>
                                </ul>
                            </div>
                        </td>
                        <td style="padding-left: 2px;">
                            <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                        </td>
                        <td style="padding-left: 5px;">
                            <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="toolbar">
                <div class="btn-group">
                    <a id="lr-replaceParts" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                    <a id="lr-addParts" class="btn btn-default" onclick="btn_addParts()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                    <a id="lr-editParts" class="btn btn-default" onclick="btn_editParts()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                    <a id="lr-deleteParts" class="btn btn-default" onclick="btn_deleteParts()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                </div>

            </div>
        </div>
        <div class="gridPanel">
            <table id="gridTable"></table>
            <div id="gridPager"></div>
        </div>
    </div>
</div>
<div id="DevicePartDetail" style="padding-left:20px;padding-right:20px;">
    <table class="form" style="margin-top:10px;">
        <tr>
            <th class="formTitle">单价<font face="宋体">*</font></th>
            <td class="formValue">
                <div>
                    <input id="UnitPrice" type="number" class="form-control" isvalid="yes" checkexpession="NotNull" />
                </div>
            </td>
            <td class="formTitle">数量<font face="宋体">*</font></td>
            <td class="formValue">
                <input id="Num" type="number" class="form-control" isvalid="yes" checkexpession="NotNull" />
            </td>
        </tr>
        <tr>
            <td class="formTitle">金额</td>
            <td class="formValue">
                <input id="Total" type="number" class="form-control" />
            </td>
            <td class="formTitle">保修厂商</td>
            <td class="formValue">
                <input id="Verdors" type="text" class="form-control" />
            </td>
        </tr>
        <tr>
            <td class="formTitle">保修开始时间</td>
            <td class="formValue">
                <input id="BeginDate" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd', maxDate: '#F{$dp.$D(\'BeginDate\')}' })" />
            </td>
            <td class="formTitle">保修结束时间</td>
            <td class="formValue">
                <input id="EndDate" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd', minDate: '#F{$dp.$D(\'EndDate\')}' })"  />
            </td>
        </tr>
        <tr>
            <th class="formTitle">
                附加说明
            </th>
            <td class="formValue" colspan="3">
                <textarea id="Description" class="form-control" style="height: 70px;"></textarea>
            </td>
        </tr>
    </table>
</div>